package com.yudaob2b.elong.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yudaob2b.business.entity.po.ElongHotelInventoryPO;
import com.yudaob2b.business.service.ElongHotelInventoryService;
import com.yudaob2b.elong.constant.RedisConstant;
import com.yudaob2b.elong.entity.dto.ELaElongHotelInvDTO;
import com.yudaob2b.elong.entity.vo.Inventory;
import com.yudaob2b.elong.enums.ela.EnumElasticElongIndexName;
import com.yudaob2b.elong.service.HotelInventoryService;
import org.apache.commons.lang.StringUtils;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存入库线程
 *
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/20/18 8:42 PM
 */
public class DataInvLocalDbTask implements Runnable, Serializable {
    private static Logger dataInvLocalDbTaskLogger = LoggerFactory.getLogger("dataInvLocalDbTaskLogger");

    private List<Inventory> invList;
    private ElongHotelInventoryService elongHotelInventoryService;
    private RedisTemplate redisTemplate;
    private HotelInventoryService hotelInventoryService;
    private ClientInterface clientUtil;

    public DataInvLocalDbTask(List<Inventory> invList, ElongHotelInventoryService elongHotelInventoryService
            , RedisTemplate redisTemplate, HotelInventoryService hotelInventoryService, ClientInterface clientUtil) {
        this.invList = invList;
        this.elongHotelInventoryService = elongHotelInventoryService;
        this.redisTemplate = redisTemplate;
        this.hotelInventoryService = hotelInventoryService;
        this.clientUtil = clientUtil;
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();

        try {
            // 待插入列表
            //List<ElongHotelInventoryPO> newList = new ArrayList();
            List<ELaElongHotelInvDTO> newList = new ArrayList();
            // 待更新列表
            List<ElongHotelInventoryPO> updateList = new ArrayList();
            // 缓存就酒店库存列表
            List<ElongHotelInventoryPO> oldElongHotelInvList = null;
            String hotelIdTemp = "";
            //long start2 = System.currentTimeMillis();
            for (Inventory inventory : invList) {
                // 状态为true，有库存，可以超售，才处理
                if (inventory.isStatus() == true && inventory.getAmount() != 0 && inventory.getOverBooking() == 0) {
                    ELaElongHotelInvDTO eLaElongHotelInvDTO = hotelInventoryService.getElaDtoByInventory(inventory);
                    newList.add(eLaElongHotelInvDTO);
                    /*
                    ElongHotelInventoryPO elongHotelInventoryPO = hotelInventoryService.getPoByInventory(inventory);
                    if (StringUtils.isEmpty(hotelIdTemp) || !hotelIdTemp.equals(elongHotelInventoryPO.getHotelId())) {
                        hotelIdTemp = elongHotelInventoryPO.getHotelId();
                        EntityWrapper<ElongHotelInventoryPO> ew = new EntityWrapper<>();
                        ew.where("hotel_id={0}", elongHotelInventoryPO.getHotelId());
                        oldElongHotelInvList = elongHotelInventoryService.selectList(ew);
                    }

                    // 检查当前ElongHotelInventoryPO是否已经存在
                    if (oldElongHotelInvList != null && oldElongHotelInvList.size() > 0) {
                        int flag = 0;
                        for (ElongHotelInventoryPO cacheInv : oldElongHotelInvList) {
                            if (cacheInv.getHotelId().equals(elongHotelInventoryPO.getHotelId())
                                    && cacheInv.getRoomTypeId().equals(elongHotelInventoryPO.getRoomTypeId())
                                    && cacheInv.getHotelCode().equals(elongHotelInventoryPO.getHotelCode())
                                    && cacheInv.getDate().compareTo(elongHotelInventoryPO.getDate()) == 0) {
                                updateList.add(elongHotelInventoryPO);
                                flag = 1;
                                break;
                            }
                        }
                        if (flag == 0) {
                            newList.add(elongHotelInventoryPO);
                        }
                    } else {
                        newList.add(elongHotelInventoryPO);
                    }*/
                }
            }
            //dataInvLocalDbTaskLogger.info("---库存线程处理数据耗时：---{}", System.currentTimeMillis() - start2);

            long start3 = System.currentTimeMillis();
            if(newList.size() > 0) {
                clientUtil.addDocuments(EnumElasticElongIndexName.INVENTORY.getIndexName(),//索引表
                        EnumElasticElongIndexName.INVENTORY.getType(),//索引类型
                        newList);
            }

            /*if (newList.size() > 0) {
                elongHotelInventoryService.replaceInto(newList);
            }
            if (updateList.size() > 0) {
                elongHotelInventoryService.updateBatch(updateList);
            }*/
            dataInvLocalDbTaskLogger.info("---库存入库线程处理数据耗时：-insert:{}, update:{}--{}", newList.size(), updateList.size(), System.currentTimeMillis() - start3);
        } catch (Exception e) {
            dataInvLocalDbTaskLogger.info("全量库存入库线程异常：", e);
            e.printStackTrace();
        }

        //dataInvLocalDbTaskLogger.info("---库存入库线程总耗时：-insert:{}, update:{}--{}", newList.size(), updateList.size(), System.currentTimeMillis() - start);
    }
}
